package com.lzs.Controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzs.PO.Disease;
import com.lzs.PO.Page;
import com.lzs.Service.DiseaseService;
import com.lzs.util.Msg;

@Controller
@RequestMapping(value = "/disease")
public class DiseaseController {
   @Autowired
   DiseaseService diseaseService;
   
	@RequestMapping(value = "/disease_list")
	public String diseaseList(Model model, HttpServletRequest request,
			@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize,
			String keyword) throws Exception {
		
		Page<Disease> page = this.diseaseService.findDiseasePagelist(keyword, pageSize, pageNum);

		model.addAttribute("page", page);

		System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!");
		return "/disease/disease_list";
	}
	@RequestMapping(value = "/disease_add")
	public String addDisease() {
		
		return"/disease/disease_add";
	}
	@ResponseBody
	@RequestMapping(value = "/disease_add_action")
	public Msg addDiseaseAction(String disease_id,String pinyin_id,String disease_name) {
		
		try {
			
			Map<String,Object>map = new HashMap<String,Object>();
			
			map.put("disease_id", disease_id);
			
			map.put("pinyin_id", pinyin_id);
			
			map.put("disease_name", disease_name);
			
			this.diseaseService.addDisease(map);
			
			return Msg.success();
			
		}catch(Exception e) {
			
			e.printStackTrace();
			
			return Msg.fail();
			
		}
		
		
	}
	@RequestMapping(value = "/disease_update")
	public String updateDisease(String disease_id,Model model) {
		
		Disease disease = this.diseaseService.findDiseaseById(disease_id);
		
		model.addAttribute("disease", disease);
		
		return"/disease/disease_update";
	}
	@ResponseBody
	@RequestMapping(value = "/disease_update_action")
	public Msg updateDisease(String disease_id,String pinyin_id,String disease_name) {
		
		try {
			
		Map<String,Object>map = new HashMap<String,Object>();
		
		map.put("disease_id", disease_id);
		
		map.put("pinyin_id", pinyin_id);
		
		map.put("disease_name", disease_name);
		
		this.diseaseService.updateDisease(map);
		
		return Msg.success();
		 
			
		}catch(Exception e) {
			
			e.printStackTrace();
			
			return Msg.fail();
			
		}
	}
	@ResponseBody
	@RequestMapping(value = "/disease_delete")
	public Msg deleteDisease(String disease_id) {
		
		try {
			
			this.diseaseService.deleteDisease(disease_id);
			
			return Msg.success();
			
		}catch(Exception e) {
			
			e.printStackTrace();
			
			return Msg.fail();
			
		}
	
	}
	@ResponseBody
	@RequestMapping(value = "/disease_check")
	public Msg checkDisease(String disease_id) {
		
		Boolean b = this.diseaseService.countDiseaseById(disease_id);
		
		if(b) {
			
			return Msg.success();
			
		}else {
			
			return Msg.fail();
			
		}
		
	}
}
